import errorEvent from '../../errorMessage';
import store from '@/store/index';
import httpService from '../../httpService';


export default {
    async getAll() {

        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/cache',
                params: {
                    projectId: projectId,
                }
            });
            return {
                res: data,
                localRes: data,
            };
        } catch (error) {
            errorEvent(error);
            return false;
        }

    },
    async creat(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'post',
                url: 'http://' + serviceAddress + '/cache',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    projectId: projectId,
                    value: param.value,
                    key: param.key,
                    name: param.name,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };

    },
    async update(param) {
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/cache',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    cacheId: param.id,
                    value: param.value,
                    key: param.key,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async delete(id) {
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'delete',
                url: 'http://' + serviceAddress + '/cache',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    cacheId: id.id,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };

    },
}